技术分享 | 使用备份恢复实例时存在的坑
作者:林靖华
爱可生服务团队成员,负责处理客户在MySQL日常运维中遇到的问题;擅长处理备份相关的问题,对数据库相关技术有浓厚的兴趣,喜欢钻研各种问题。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
-- master(131)
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> create database test2;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 478 | | | eefac7d8-2370-11e9-bfeb-000c29d74445:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
-- slave(132)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.13.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 478
Relay_Log_File: 192-168-13-132-relay-bin.000002
Relay_Log_Pos: 691
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
Retrieved_Gtid_Set: eefac7d8-2370-11e9-bfeb-000c29d74445:1-2
Executed_Gtid_Set: eefac7d8-2370-11e9-bfeb-000c29d74445:1-2
2、 在从库上创建备份
mysqldump -uroot -p123456 --single-transaction --master-data=2 --all-databases > dump.sql
3、向主库写入数据
-- master(131)
mysql> create database test3;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
| test3 |
+--------------------+
7 rows in set (0.01 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 640 | | | eefac7d8-2370-11e9-bfeb-000c29d74445:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
-- slave(132)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
| test3 |
+--------------------+
7 rows in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.13.131
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 640
Relay_Log_File: 192-168-13-132-relay-bin.000002
Relay_Log_Pos: 853
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
......
Retrieved_Gtid_Set: eefac7d8-2370-11e9-bfeb-000c29d74445:1-3
Executed_Gtid_Set: eefac7d8-2370-11e9-bfeb-000c29d74445:1-3
4、产生故障
-- new master(132)
mysql> create database test4;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
| test3 |
| test4 |
+--------------------+
8 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| mysql-bin.000001 | 802 | | | b66b4623-207d-11ea-a993-000c29122c12:1,
eefac7d8-2370-11e9-bfeb-000c29d74445:1-3 |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
5、恢复实例
-- old master(131)
mysql -uroot -p123456 < dump.sql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
+--------------------+
6 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000001 | 478 | | | eefac7d8-2370-11e9-bfeb-000c29d74445:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
6、与新主建立复制关系
-- old master(131)
mysql> change master to
-> master_host='192.168.13.132',
-> master_user='repl',
-> master_password='123456',
-> master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test1 |
| test2 |
| test4 |
+--------------------+
7 rows in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
| mysql-bin.000001 | 640 | | | b66b4623-207d-11ea-a993-000c29122c12:1,
eefac7d8-2370-11e9-bfeb-000c29d74445:1-2 |
+------------------+----------+--------------+------------------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
eefac7d8-2370-11e9-bfeb-000c29d74445:3
,我们先到新主的 binlog 中看下是否还有这个事务的记录,保证 binlog 没有被清理。#200220 14:19:41 server id 1 end_log_pos 543 CRC32 0x3fa9fb8c GTID last_committed=2 sequence_number=3 rbr_only=no
SET @@SESSION.GTID_NEXT= 'eefac7d8-2370-11e9-bfeb-000c29d74445:3'/*!*/;
# at 543
#200220 14:19:41 server id 1 end_log_pos 640 CRC32 0x8427b0a3 Query thread_id=4 exec_time=0 error_code=0
SET TIMESTAMP=1582179581/*!*/;
create database test3
/*!*/;
eefac7d8-2370-11e9-bfeb-000c29d74445:1-2
两个事务,那么恢复回来的实例加入集群变成从实例的时候,需要通过新主库的 binlog 来补偿数据。eefac7d8-2370-11e9-bfeb-000c29d74445:3
的时候,从实例的 io_thread 发现这个事务记录的 server_id 为 1,与自己的 server_id 一致,会认为这个是自己执行过的事务,就会把这段信息给过滤掉,最终这个从实例的数据就会缺少一部分。--replicate-same-server-id
,这个参数的作用是开启后即使 io_thread 收到与自己 server_id 相同的 binlog,也会写入 relaylog。虽然启用这个参数也可以避免以上问题,但在大部分情况下都不推荐开启这个参数,默认值 OFF 是为了避免 binlog 在复制中回环。在 5.7 中想开启这个参数的话需要先关闭 log_slave_updates
;8.0 则做了改进,当 gtid_mode=ON
的情况下就可以开启。参考:
https://lefred.be/content/mysql-gtid-restore-a-master-from-a-replicas-backup/ https://www.zhangshengrong.com/p/yOXD5zZM1B/ https://dev.mysql.com/doc/refman/5.7/en/replication-options-slave.html#optionmysqldreplicate-same-server-id
社区近期动态